<template>
  <div id="app">
    <el-container>
        <el-header>
          <div class="header">
            <div class="logo" @click="$router.push('/')">
              <h1>📚 二手教材交易平台</h1>
            </div>
            <div class="header-right">
              <el-menu
                mode="horizontal"
                :default-active="$route.path"
                router
                class="nav-menu"
              >
                <el-menu-item index="/">首页</el-menu-item>
                <el-menu-item index="/textbooks">教材市场</el-menu-item>
                <el-menu-item v-if="currentUser" index="/publish">发布教材</el-menu-item>
                <el-menu-item v-if="currentUser" index="/orders">我的订单</el-menu-item>
              </el-menu>
              <div class="user-info">
                <span v-if="currentUser" class="user-name">
                  欢迎，<span>{{ currentUser.username || '用户' }}</span>
                </span>
                <el-button 
                  v-if="!currentUser"
                  type="primary" 
                  size="small"
                  @click="handleLogin"
                >
                  登录
                </el-button>
                <el-dropdown v-if="currentUser" @command="handleCommand">
                  <el-button size="small">
                    个人中心
                    <el-icon class="el-icon--right">
                      <arrow-down />
                    </el-icon>
                  </el-button>
                  <template #dropdown>
                    <el-dropdown-menu>
                        <el-dropdown-item command="orders">我的订单</el-dropdown-item>
                        <el-dropdown-item command="publish">发布教材</el-dropdown-item>
                        <el-dropdown-item divided command="logout">退出登录</el-dropdown-item>
                      </el-dropdown-menu>
                  </template>
                </el-dropdown>
              </div>
            </div>
          </div>
        </el-header>
      
      <el-main>
        <router-view />
      </el-main>
      
        <el-footer>
          <div class="footer">
            <p>&copy; 2024 二手教材交易平台. All rights reserved.</p>
          </div>
        </el-footer>
    </el-container>
  </div>
</template>

<script setup>
import { ref, onMounted, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { ArrowDown } from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()
const currentUser = ref(null)

// 处理登录
const handleLogin = () => {
  router.push('/login')
}

// 处理下拉菜单命令
const handleCommand = (command) => {
  switch (command) {
    case 'orders':
      router.push('/orders')
      break
    case 'publish':
      router.push('/publish')
      break
    case 'logout':
      handleLogout()
      break
  }
}

// 处理退出登录
const handleLogout = () => {
  localStorage.removeItem('user')
  currentUser.value = null
  ElMessage.success('退出成功')
  router.push('/')
}

// 初始化用户状态
onMounted(() => {
  console.log('二手教材交易平台已启动')
  
  // 从本地存储获取用户信息
  const userStr = localStorage.getItem('user')
  if (userStr) {
    try {
      currentUser.value = JSON.parse(userStr)
    } catch (e) {
      localStorage.removeItem('user')
    }
  }
})

// 监听路由变化，更新用户状态
watch(() => route.path, () => {
  const userStr = localStorage.getItem('user')
  if (userStr) {
    try {
      currentUser.value = JSON.parse(userStr)
    } catch (e) {
      localStorage.removeItem('user')
      currentUser.value = null
    }
  } else {
    currentUser.value = null
  }
})
</script>

<style scoped>
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  width: 100%;
}

.logo {
  cursor: pointer;
}

.logo h1 {
  margin: 0;
  font-size: 24px;
  font-weight: bold;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 20px;
}

.nav-menu {
  background-color: transparent;
  border-bottom: none;
}

.nav-menu .el-menu-item {
  color: white;
  border-bottom: 2px solid transparent;
}

.nav-menu .el-menu-item:hover,
.nav-menu .el-menu-item.is-active {
  background-color: rgba(255, 255, 255, 0.1);
  border-bottom-color: white;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 10px;
}

.user-name {
  color: #fff;
  font-size: 16px;
}

.footer {
  text-align: center;
  padding: 20px;
  background-color: #f5f5f5;
  color: #666;
}

#app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.el-container {
  min-height: 100vh;
}

.el-main {
  padding: 20px;
  background-color: #fafafa;
  min-height: calc(100vh - 120px);
}
</style>


